package com.juanbonfante.components 
{
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.geom.Matrix;
	import flash.display.GradientType;
	import flash.display.DisplayObject;

	import com.greensock.TweenLite;

	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.utils.Timer;

	import com.juanbonfante.components.main.buttons.DirectionArrow;
	import com.greensock.plugins.TintPlugin;
	import com.greensock.plugins.TweenPlugin;

	import flash.display.Sprite;
/**
 * @author jbonfante
 * com.juanbonfante.components.AbstractComponent
 * Created on: 8:10:55 PM , 2010 
 */

/**
	 * 	Simple UI Component Framework for AS3
	 
	 	The MIT License

		Copyright (c) 2010 Juan Bonfante
		
		Contributor(s):
		Juan Bonfante
		
		Permission is hereby granted, free of charge, to any person obtaining a copy
		of this software and associated documentation files (the "Software"), to deal
		in the Software without restriction, including without limitation the rights
		to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
		copies of the Software, and to permit persons to whom the Software is
		furnished to do so, subject to the following conditions:
		
		The above copyright notice and this permission notice shall be included in
		all copies or substantial portions of the Software.
		
		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
		IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
		FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
		AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
		LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
		OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
		THE SOFTWARE.
	 */
	 
	 /***
	  * This is a DEMO Class. To show the basic principle. This component has a set
	  * width, but changes in height according to stage size.
	  * Any content can be used for display.
	  */
	
	 
	public class AbstractComponent extends Sprite 
	{
//		
//		 * TODO: Create Absctract Constants.
//		 * TODO: Create IComponent Interface
//		 * TODO: Create Settings file to allow for color changes, and different clips to be used as buttons
//		 * TODO: Create class(s) for scrolling components
//		 * TODO: Create horizontal scroller + logic.
//		 * TODO: Create horizontal/vertical blend scroller
//		 * TODO: Create option to make sizing set, or dynamic
//		 * TODO: Refactor this class into smaller pieces.
//		 * TODO: Create DirectionArrowFactory
//		 * TODO: Create ScrollerFactory
//		 * TODO: Create TrackFactory
//		 * TODO: Create ScrollingManager Class
//		 
		protected static const MARGIN : Number = 10;
		protected static const CONTENT_DEFAULT_YPOS : Number = 20;
		protected var COMPONENT_CONTENT : Sprite = new Sprite();
		protected var COMPONENT_CONTENTS_ARRAY : Array = [];
		protected var COMPONENT_MASK : Sprite = new Sprite();
		protected static var _MaskHeight : Number = 320;
		protected static var _MaskWidth : Number = 346;
		protected var COMPONENT_VERTICAL_SCROLLBAR : Sprite = new Sprite();
		protected var COMPONENT_SCROLL_TRACK : Sprite = new Sprite();
		protected var COMPONENT_SCROLL_THUMB : Sprite = new Sprite();
		protected var _ScrollYOffset : Number;
		protected var yMin : int;
		protected var yMax : Number;
		protected var scrollPercent : Number = 0;
		protected var scrollUp : Sprite;
		protected var scrollDown : Sprite;
		protected var currentMaskHeight : Number;
		protected var thumbPercent : Number;
		protected var arrowTimer : Timer = new Timer(100);
		
		TweenPlugin.activate([TintPlugin]);
		private var _scrollDirection : Boolean;

		public function AbstractComponent()
		{
			COMPONENT_CONTENT.y = CONTENT_DEFAULT_YPOS;
			COMPONENT_CONTENT.x = 10;
			currentMaskHeight = _MaskHeight;
			addChild(COMPONENT_CONTENT);
			
			
			
			addEventListener(Event.ADDED_TO_STAGE, setupListeners, false, 0, true);
			Fill(300, 500);
		}

		private function Fill(width : int, height : int) : void 
		{
			var aSprite : Sprite = new Sprite();
			aSprite.graphics.beginGradientFill(GradientType.LINEAR, [0x000000,0xDD00DD], [0.9,0.1], [0,255], new Matrix(0, -1, 1, 0, height, 0));
			aSprite.graphics.drawRect(0, 0, width, height);
			aSprite.graphics.endFill();
			
			init([aSprite]);
		}

		public function init(defaultData : Array = null) : void 
		{
			if(defaultData == null)
			{
				clear();
				return;
			}
			try
			{
				"clearning";
				clear();
			}
			finally
			{
				trace("Finalizing");
				COMPONENT_CONTENTS_ARRAY = [];
				for(var i : int = 0;i < defaultData.length;i++)
				{
					var defaultObject : DisplayObject = DisplayObject(defaultData[i]);
					if(i > 0)
					{
						var prevChild : DisplayObject = COMPONENT_CONTENT.getChildAt(i - 1);
						defaultObject.y = prevChild.getBounds(this).bottom - (defaultObject.height) + 15; 
					}
					COMPONENT_CONTENT.addChild(defaultObject);
					COMPONENT_CONTENTS_ARRAY.push(defaultObject);
					//refreshScroll();
					createMask(_MaskWidth, (stage.stageHeight - 100) - 206.95 - 86);
				}
				COMPONENT_SCROLL_THUMB.y = 0;
				scrollPercent = 0;
			
				refreshScroll();
			}
		}

		private function CheckScrollHeight() : void 
		{
			if(COMPONENT_CONTENT.height < currentMaskHeight) 
			{
				COMPONENT_VERTICAL_SCROLLBAR.visible = false;
				scrollPercent = 0;
				refreshScroll();
			}
			else COMPONENT_VERTICAL_SCROLLBAR.visible = true;
		}

		private function clear() : void 
		{
			var cLength : int = COMPONENT_CONTENTS_ARRAY.length;
			if(cLength > 0)
			{
				for(var i : int ;i < cLength ;i++)
				{
					COMPONENT_CONTENT.removeChild(COMPONENT_CONTENTS_ARRAY[i]);
				}
			}
			COMPONENT_SCROLL_THUMB.y = 0;
			COMPONENT_CONTENT.y = CONTENT_DEFAULT_YPOS;
			
			CheckScrollHeight();
		}

		private function setupListeners(event : Event) : void 
		{
			buildScrollBar();
			createMask(_MaskWidth, _MaskHeight);
			stage.addEventListener(Event.RESIZE, draw, false, 0, true);
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
		}

		private function draw(event : Event) : void 
		{
			
			var newHeight : Number = (stage.stageHeight - 100) - 206.95 - 86;
			COMPONENT_SCROLL_TRACK.graphics.clear();
			COMPONENT_SCROLL_TRACK.graphics.beginFill(0x222222);
			COMPONENT_SCROLL_TRACK.graphics.drawRect(0, 0, 15, TrackHeight(newHeight + 15));
			COMPONENT_SCROLL_TRACK.graphics.endFill();
			createMask(_MaskWidth, newHeight + 10);
			scrollDown.y = ScrollDownPosition(COMPONENT_SCROLL_TRACK.height);
			
			refreshScroll();
			//CheckScrollHeight();
		}

		private function TrackHeight(newHeight : Number) : Number 
		{
			return newHeight - 30;
			;
		}

		private function refreshScroll() : void 
		{
			yMin = 0;
			yMax = COMPONENT_SCROLL_TRACK.height - COMPONENT_SCROLL_THUMB.height;	
			if(COMPONENT_CONTENT.height > COMPONENT_MASK.height)thumbPercent = (COMPONENT_MASK.height / COMPONENT_CONTENT.height );
			
			//if(thumb.height >= 20)
			//{
			COMPONENT_SCROLL_THUMB.graphics.clear();
			COMPONENT_SCROLL_THUMB.graphics.beginFill(0xCCCCCC);
			
			COMPONENT_SCROLL_THUMB.graphics.drawRect(0, 0, 11, thumbPercent > .2 ? COMPONENT_SCROLL_TRACK.height * thumbPercent : 20);
			COMPONENT_SCROLL_THUMB.graphics.endFill();
			//if(thumb.y > track.height - thumb.height)thumb.y = track.height - thumb.height;
			//else(scrollPercent = 0);
			//}
			//scrollPercent == 0 ? thumb.y = 0 : thumb.y = track.height * scrollPercent;
			COMPONENT_CONTENT.y = Math.round(-scrollPercent * (COMPONENT_CONTENT.height - COMPONENT_MASK.height ) + MARGIN + 6);
			COMPONENT_SCROLL_THUMB.y = yMax * scrollPercent;
			if (COMPONENT_SCROLL_THUMB.y <= yMin) 
			{
				COMPONENT_SCROLL_THUMB.y = yMin;
				//stackContent.y = CONTENT_DEFAULT_YPOS;
			}
			
			if (COMPONENT_SCROLL_THUMB.y >= yMax) 
			{
				COMPONENT_SCROLL_THUMB.y = yMax;
				//stackContent.y = Math.round(-1 * (stackContent.height - contentMask.height ) + MARGIN + 6);
			}
			//else stackContent.y = CONTENT_DEFAULT_YPOS;
		}

		private function createMask(mWidth : Number, mHeight : Number) : void 
		{
			var g : Graphics = COMPONENT_MASK.graphics;
			g.clear();
			g.beginFill(0xff);
			g.drawRect(0, 0, mWidth, mHeight);
			g.endFill();
			currentMaskHeight = mHeight;
			COMPONENT_MASK.x = -10;
			COMPONENT_MASK.y = 20;
			addChild(COMPONENT_MASK);
			COMPONENT_CONTENT.mask = COMPONENT_MASK;
			CheckScrollHeight();
		}

		private function buildScrollBar() : void 
		{

			
			addChild(COMPONENT_VERTICAL_SCROLLBAR);
			COMPONENT_VERTICAL_SCROLLBAR.x = 332;
			COMPONENT_VERTICAL_SCROLLBAR.y = 36;

			
			COMPONENT_VERTICAL_SCROLLBAR.addChild(COMPONENT_SCROLL_TRACK);
			COMPONENT_SCROLL_TRACK.graphics.beginFill(0x222222);
			COMPONENT_SCROLL_TRACK.graphics.drawRect(0, 0, 15, TrackHeight(_MaskHeight));
			COMPONENT_SCROLL_TRACK.graphics.endFill();

			
			COMPONENT_VERTICAL_SCROLLBAR.addChild(COMPONENT_SCROLL_THUMB);
			COMPONENT_SCROLL_THUMB.x = 2;
			COMPONENT_SCROLL_THUMB.graphics.beginFill(0xCCCCCC);
			
			COMPONENT_SCROLL_THUMB.graphics.drawRect(0, 0, 11, 30);
			COMPONENT_SCROLL_THUMB.graphics.endFill();
			
			scrollUp = new DirectionArrow();
			//scrollUp.y = -scrollUp.height/2;
			scrollUp.x = 7.5;
			scrollUp.y = -8;
			COMPONENT_VERTICAL_SCROLLBAR.addChild(scrollUp);
			
			scrollDown = new DirectionArrow();
			scrollDown.x = 7.5;
			
			scrollDown.rotation = 180;
			scrollDown.y = ScrollDownPosition(COMPONENT_SCROLL_TRACK.height);
			COMPONENT_VERTICAL_SCROLLBAR.addChild(scrollDown);			
			

			var dropShadow : DropShadowFilter = new DropShadowFilter();
			dropShadow.color = 0x000000;
			dropShadow.blurX = 10;
			dropShadow.blurY = 10;
			dropShadow.angle = 90;
			dropShadow.alpha = 0.9;
			dropShadow.distance = 3;

			//var filtersArray : Array = new Array(dropShadow);
			//thumb.filters = filtersArray;
			COMPONENT_SCROLL_THUMB.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
			COMPONENT_SCROLL_THUMB.addEventListener(MouseEvent.MOUSE_OVER, thumbOver);
			COMPONENT_SCROLL_THUMB.addEventListener(MouseEvent.MOUSE_OUT, thumbOut);
			stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);
			scrollDown.addEventListener(MouseEvent.CLICK, ArrowScrollDown, false, 0, true);
			scrollDown.addEventListener(MouseEvent.MOUSE_DOWN, StartDownTimer, false, 0, true);
			scrollDown.addEventListener(MouseEvent.MOUSE_UP, StopTimer, false, 0, true);
			scrollUp.addEventListener(MouseEvent.MOUSE_DOWN, StartUpTimer, false, 0, true);
			scrollUp.addEventListener(MouseEvent.MOUSE_UP, StopTimer, false, 0, true);
			scrollUp.addEventListener(MouseEvent.CLICK, ArrowScrollUp, false, 0, true);
			
			arrowTimer.addEventListener(TimerEvent.TIMER, ChangeScrollBar, false, 0, true);
		}

		private function ChangeScrollBar(event : TimerEvent) : void 
		{
			if(scrollPercent > 0 && scrollPercent < 1)
			{
				
				_scrollDirection ? scrollPercent -= .1 : scrollPercent += .1;
				refreshScroll();
			}
			else if (scrollPercent == 0)
			{
				scrollPercent = 0;
				refreshScroll();
			}
			else if(scrollPercent == 1)
			{
				scrollPercent = 1;
				refreshScroll();
			}
		}

		private function ControlTimer(signal :Sprite = null) : void
		{
			switch(signal)
			{
				case scrollDown:
					_scrollDirection = false;
					arrowTimer.start();
					break;
					
				case scrollUp:
					_scrollDirection = true;
					arrowTimer.start();
					
					break;
					
				case null:
				default:
					arrowTimer.stop();
					arrowTimer.reset();
					break;
			}
		}

		private function StartDownTimer(event : MouseEvent) : void 
		{
			ControlTimer(scrollDown);
		}

		private function StartUpTimer(event : MouseEvent) : void 
		{
			ControlTimer(scrollUp);
		}

		private function StopTimer(event : MouseEvent) : void 
		{
			ControlTimer();
		}

		private function ArrowScrollUp(event : MouseEvent) : void 
		{
			
			scrollPercent > 0 ? scrollPercent -= .1 : 0;
			refreshScroll();
		}

		private function ArrowScrollDown(event : MouseEvent) : void 
		{
			
			scrollPercent < 1 ? scrollPercent += .1 : 1;
			refreshScroll();
		}

		private function thumbOut(event : MouseEvent) : void 
		{
			TweenLite.to(COMPONENT_SCROLL_THUMB, .5, {tint:0xCCCCCC});
		}

		private function thumbOver(event : MouseEvent) : void 
		{
			TweenLite.to(COMPONENT_SCROLL_THUMB, 0, {tint:0x999999});
		}

		private function ScrollDownPosition(height : Number) : Number 
		{
			return height + 7;
		}

		private function thumbDown(event : MouseEvent) : void 
		{
			yMin = 0;
			yMax = COMPONENT_SCROLL_TRACK.height - COMPONENT_SCROLL_THUMB.height;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
			scrollYOffset = mouseY - COMPONENT_SCROLL_THUMB.y;
			TweenLite.to(COMPONENT_SCROLL_THUMB, 0, {tint:0xFFFFFF});
		}

		private function thumbMove(event : MouseEvent) : void 
		{
			COMPONENT_SCROLL_THUMB.y = mouseY - scrollYOffset;
			if (COMPONENT_SCROLL_THUMB.y <= yMin) 
			{
				COMPONENT_SCROLL_THUMB.y = yMin;
			}
			if (COMPONENT_SCROLL_THUMB.y >= yMax) 
			{
				COMPONENT_SCROLL_THUMB.y = yMax;
			}

			scrollPercent = COMPONENT_SCROLL_THUMB.y / yMax;
			COMPONENT_CONTENT.y = Math.round(-scrollPercent * (COMPONENT_CONTENT.height - COMPONENT_MASK.height ) + MARGIN + 6);
			event.updateAfterEvent();
		}

		private function thumbUp(event : MouseEvent) : void 
		{
			if(stage)stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
			TweenLite.to(COMPONENT_SCROLL_THUMB, 0, {tint:0xCCCCCC});
		}

		static public function get maskHeight() : Number
		{
			return _MaskHeight;
		}

		static public function set maskHeight(maskHeight : Number) : void
		{
			_MaskHeight = maskHeight;
		}

		static public function get maskWidth() : Number
		{
			return _MaskWidth;
		}

		static public function set maskWidth(maskWidth : Number) : void
		{
			_MaskWidth = maskWidth;
		}

		public function get scrollYOffset() : Number
		{
			return _ScrollYOffset;
		}

		public function set scrollYOffset(scrollYOffset : Number) : void
		{
			_ScrollYOffset = scrollYOffset;
		}
	}
}
